#ifndef PROFTPD_MONITOR_H
#define PROFTPD_MONITOR_H

#include "../ftpmonitorloop.h"

/*
 *
 *
*/
class ProftpdMonitor : public FTPMonitorLoop
{
	public:
		/*
		 *
		 */
		ProftpdMonitor(QString whoPath,bool useSudo)
		:FTPMonitorLoop()
		{
			createExecCommand(useSudo,whoPath);
		};

		/*
		 * 	12391 ftp      [ 1m02s]  0m10s idle client: _none_ [::ffff:127.0.0.1] server: 127.0.0.2:21 (ProFTPD Anonymous Server) location: 
		 * 	12391 ftp      [ 3m10s] (  0%) RETR /bigfile.test KB/s: 40700.00 client: _none_ [::ffff:127.0.0.1] server: 127.0.0.2:21 (ProFTPD Anonymous Server) location: 
		 *
		 */
		void processStdOut(QString stdout)
		{
			QRegExp rx;
			QVector<QStringList> listOfClients;
			QStringList clientRow;
			
			QStringList raw_string_list = QString(stdout).split("\n");
			
			QStringList::Iterator it = raw_string_list.begin();
			if(it == raw_string_list.end())
				return;
			
			for ( it++ ; it != raw_string_list.end() ; it++ ) {
				rx.setPattern("^(\\d+) (\\S+) .*s........ (.*) client:(.*) server:(.*) location:(.*)$");
				if (rx.indexIn(it->trimmed()) != -1) {
					clientRow.clear();
					clientRow << rx.cap(1) << rx.cap(2) << rx.cap(3) << rx.cap(4) << rx.cap(6);
					listOfClients.push_back(clientRow);
				}
			}
			
			searchForClientsStateChange(listOfClients);
		}

		/*
		 *
		 */
		void createExecCommand(bool useSudo,QString whoPath)
		{

			if (whoPath=="")
				mFtpWhoCommand.bin = "/usr/sbin/" + ftpWhoBinaryName();
			else
				mFtpWhoCommand.bin = whoPath + ftpWhoBinaryName();

			mFtpWhoCommand.args << "-v" << "-o" << "oneline";
			//if we use sudo, then ftpwho binary is first argumen of sudo command
			if (useSudo==true)
			{
				mFtpWhoCommand.args.prepend(mFtpWhoCommand.bin);
				//The -n (non-interactive) option prevents sudo from prompting the user
				//for a password.  If a password is required for the command to run, sudo
				//will display an error messages and exit.
				mFtpWhoCommand.args.prepend("-n");
				mFtpWhoCommand.bin="sudo";
			}
	
		}

		/*
		 *
		 */
		bool ftpClientDiff(QStringList newClientRow,QStringList oldClientRow)
		{
			if (newClientRow.at(2) != oldClientRow.at(2) || newClientRow.at(4) != oldClientRow.at(4))
				return true;
			else
				return false;
		}
		/*
		 *
		 */
		QStringList headerLabels()
		{
			return QStringList() << "PID" << "Login" << "What" << "Client" << "Location";
		}

		/*
		 *
		 */
		FtpServerType ftpServerType()
		{
			return FTPMonitorLoop::proftpd;
		}

		/*
		 *
		 */
		QString ftpdServerName()
		{
			return QString("proftpd");
		}

		QString static ftpWhoBinaryName()
		{
			return QString("ftpwho");
		}
};

#endif
